package com.shuoyue.ycgk.views.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.baseadapter.AppBaseQuickAdapter;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.utils.GridSpacingItemDecoration;
import com.shuoyue.ycgk.utils.SizeUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TypeSelectDialog extends Dialog {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    private Context mContext;

    public TypeSelectDialog(@NonNull Context context, List<Type> mdata) {
        super(context, R.style.dialog);
        this.mContext = context;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setWindowAnimations(R.style.ScreenStyleLeftToRight);
        View view = View.inflate(context, R.layout.dialog_type_select, null);
        setContentView(view);
        ButterKnife.bind(this, view);
        getWindow().setLayout((SizeUtil.getScreenWidth(mContext) * 3) / 4, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setGravity(Gravity.LEFT);

        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 2));
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, SizeUtil.dip2px(mContext, 12), true));
        setData(mdata);
    }

  public   void setData(List<Type> baseTypes) {
        DialogAdapter adapter = new DialogAdapter(baseTypes);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            Type baseType = adapter.getItem(position);
            if (baseType.isSelect()) {
                //本身就选中了
                hide();
                return;
            }
            for (Type type : adapter.getData()) {
                type.setSelect(false);
            }
            baseType.setSelect(true);
            if (callback != null) {
                callback.select(baseType);
            }
            hide();
        });
    }


    class DialogAdapter extends AppBaseQuickAdapter<Type> {

        public DialogAdapter(@Nullable List<Type> data) {
            super(R.layout.item_select_type, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Type item) {
            helper.addOnClickListener(R.id.text);
            helper.setText(R.id.text, item.getName());
            helper.setTextColor(R.id.text, item.isSelect() ?
                    ContextCompat.getColor(mContext, R.color.option_bg_blue)
                    : ContextCompat.getColor(mContext, R.color.black));
            helper.setBackgroundRes(R.id.text, item.isSelect() ?
                    R.drawable.bg_text_blue_light
                    : R.drawable.bg_text_gray_light);
        }
    }

    public void setCallback(OnSelectCallback callback) {
        this.callback = callback;
    }

    OnSelectCallback callback;

    public interface OnSelectCallback {
        void select(Type baseType);
    }
}
